ifeq ($(wildcard config.mk),)
$(error "config.mk is not present, run configure !")
endif

include config.mk
CFLAGS += -I. -fPIC -g -Wall 

DESTDIR ?=
PREFIX ?= /usr

SRCS := shairport.c daemon.c rtsp.c mdns.c mdns_external.c common.c rtp.c metadata.c player.c alac.c
DEPS := config.mk alac.h audio.h common.h daemon.h getopt_long.h mdns.h metadata.h player.h rtp.h rtsp.h

ifeq ($(CONFIG_SNDIO), yes)
SRCS += audio_sndio.c
endif

ifeq ($(CONFIG_AO), yes)
SRCS += audio_ao.c
endif

ifeq ($(CONFIG_PULSE), yes)
SRCS += audio_pulse.c
endif

ifeq ($(CONFIG_ALSA), yes)
#SRCS += audio_alsa.c
endif

ifeq ($(CONFIG_OSS), yes)
#SRCS += audio_oss.c
endif

ifeq ($(CONFIG_AVAHI), yes)
SRCS += mdns_avahi.c
endif

ifeq ($(CONFIG_HAVE_DNS_SD_H), yes)
SRCS += mdns_dns_sd.c
endif

ifeq ($(CONFIG_FASTMDNS), yes)
SRCS += mdns_fastmdns.c
SRCS += fastmdns.c
SRCS += mcast.c

DEPS += fastmdns.h
DEPS += mcast.h
endif

ifeq ($(CONFIG_TINYSVCMDNS), yes)
SRCS += mdns_tinysvcmdns.c
SRCS += tinysvcmdns.c

DEPS += tinysvcmdns.h
endif

ifneq ($(CONFIG_HAVE_GETOPT_H), yes)
SRCS += getopt_long.c
endif

# default target
TARGETS = mdns_discover
all: $(TARGETS)

install: $(TARGETS)
	install -d $(DESTDIR)$(PREFIX)/bin
	install -m 755 mdns_discover $(DESTDIR)$(PREFIX)/bin/mdns_discover
	#install -m 755 -d $(DESTDIR)$(PREFIX)/lib
	#install -m 755 -d $(DESTDIR)$(PREFIX)/include
	#install -m 755 libshairport.so $(DESTDIR)$(PREFIX)/lib/
	#install -m 755 libshairport.a $(DESTDIR)$(PREFIX)/lib/
	#install -m 664 airplay_interface.h $(DESTDIR)$(PREFIX)/include

VERSION_=\"$(VERSION)\"
__version_file:
	@if [ ! -f version.h -o "`cat .version 2>/dev/null`" != '$(VERSION_)' ]; then \
		echo $(VERSION_) > version.h; \
	fi

%.o: %.c $(DEPS)
	$(CC) -c $(CFLAGS) $<

mdns_discover.o: __version_file

OBJS := $(SRCS:.c=.o)
mdns_discover: $(SRCS) mdns_fastmdns.c fastmdns.c mcast.c
	$(CC) $^ -lpthread -o $@ -I$(MOLIB_UPDATER_DIR)/usr/include \
	-L$(MOLIB_UPDATER_DIR)/usr/lib -lcrypto -lm

libshairport.so: $(OBJS)
	$(CC) $(OBJS) $(LDFLAGS) -shared -o $@

libshairport.a: $(OBJS)
	$(AR) rs $@ $(OBJS)

clean:
	rm -f $(TARGETS) version.h
	rm -f $(OBJS)

distclean: clean
	-rm -f config.log
	-rm -f config.mk
	-rm -f config.h

.PHONY: install clean distclean
